// pages/address/address.js
import utils from "../../utils/utils.js";
const app = getApp();

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		msgList: [],
		cardList: [],
		monthEndTime: '',
		index: 0,
		buyMonthCardUrl: '/myMonthCard/buyMonthCard',
		monthCardListUrl: '/myMonthCard/monthCardList',
		buyStatus: false,
		noBuyStatus: false,
		requestOne: false,
		cardlastImg:'/card.png',
		cardBglastImg:'/walletbg.png',
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {

	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		this.getListData();
		this.img();
	},
	img(){
		let cardImg=app.globalData.baseimg+this.data.cardlastImg;
		let cardBgImg=app.globalData.baseimg+this.data.cardBglastImg;
		this.setData({
			cardImg,
			cardBgImg,
		});
	},
	getListData() {
		let that = this;
		let indexData = my.getStorageSync({key:'indexData'});
    indexData=indexData.data;
		let userData = indexData.user;
		let _data = {
			url: that.data.monthCardListUrl,
			nonelist: true,
			methods: 'post',
			data: {
				'userId': userData.userId,
				'type': userData.type,
				'deviceId': userData.deviceCode
			}
		};
		utils.request.call(_data, (res) => {
      if(res.data.status==200){
        my.setStorageSync({key:'monthCardInfo',data:res.data.data});
        let cardImg = [];
        cardImg.push(res.data.data);
        that.data.cardList = cardImg;
        that.data.msgList = res.data.data;
        that.data.cardList.map(function(val, key) {
          if (!val.discountAmount) {
            val.discountAmount = '0.00';
          };
          if (val.isJudge) {
            that.data.noBuyStatus = true;
            that.data.buyStatus = that.data.cardList[0].isJudge;
            if (that.data.buyStatus) {
              that.data.monthEndTime = that.data.cardList[0].monthEndTime;
            };
          };
        });
        console.log(typeof that.data.msgList.isNotLimited);
        that.data.msgList.isNotLimited = Number(that.data.msgList.isNotLimited);
        this.setData({
          cardList: that.data.cardList,
          msgList: that.data.msgList,
          noBuyStatus: that.data.noBuyStatus,
          buyStatus: that.data.buyStatus
        });
      }else{
				let monthCardInfo=res.data.msg;
				my.setStorageSync({key:'monthCardInfo',data:monthCardInfo});
				my.reLaunch({
					url:'../mine/mine'
				});
			};
			
		});
	},
	cardlist(){
		my.navigateTo({
			url:'../cardRecords/cardRecords'
		});
	},
	buycard() {
		let that = this;
		if (that.data.noBuyStatus) {
			utils.tips('您已购买过月卡，不能购买其他月卡了！')
			return false;
		};
		that.data.requestOne = false;
		that.common_request_post(function(res) {
			let parameter = res.data.data;
			that.onBridgeReadyArgments(parameter);
		});
	},
	common_request_post(success) {
		let that = this;
		let cardId = that.data.cardList[that.data.index].id;
		let indexData=my.getStorageSync({key:'indexData'});
        indexData=indexData.data;
		let userData=indexData.user;
		let data = {
			'userId': userData.userId,
			'type': userData.type,
			'monthCardId': cardId,
			'deviceCode': userData.deviceCode,
			'deviceId': userData.deviceCode
		};
		let requestUrl = that.data.requestOne ? that.data.monthCardListUrl : that.data.buyMonthCardUrl;
		let _data = {
			url: requestUrl,
			data: data,
			methods: 'post',
			nonelist: true
		};
		utils.request.call(_data, (res) => {
			success(res);
		});
	},
	/*发起微信支付*/
	onBridgeReadyArgments(param) {
		let that = this;
    let paySuccess=true;
    my.setStorageSync({key:'paySuccess',data:paySuccess});
		my.tradePay({
      tradeNO: param,  
      success: function(res) {
        if ("9000" == res.resultCode) { // 调用支付成功
					that.data.requestOne=true;
					that.common_request_post(function(res) {
						if (res.data.status == 200) {
							utils.tips('支付成功');
              my.removeStorageSync({key:'paySuccess'});
							setTimeout(function() {
								that.getListData();
							}, 800);
						} else {
							utils.tips(res.data.msg);
              my.removeStorageSync({key:'paySuccess'});
						};
					});
					
				} else {
					utils.tips('支付取消！');
          my.removeStorageSync({key:'paySuccess'});
					return false;
					
				};
			},
			fail(res) {
				utils.tips('您已取消支付！');
				my.removeStorageSync({key:'paySuccess'});
				//fail
			},
			complete() {
				//complete
				
			}
		});
	},
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})
